package com.punai.config.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.punai.common.utils.sqlite.SqliteUtils;
import com.punai.config.constants.PnConfigConstants;
import com.punai.config.domain.ConfigGenerateCode;
import com.punai.config.mapper.ConfigGenerateCodeMapper;
import com.punai.config.service.ConfigGenerateCodeService;
import com.punai.config.sqlitedao.ConfigFormatLinkFeatureRepository;
import com.punai.config.sqlitedomain.ConfigEngineerLinkElement;
import com.punai.config.sqlitedomain.ConfigFormatLinkElement;
import io.netty.util.internal.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.util.Map;

@Service
public class ConfigGenerateCodeServiceImpl implements ConfigGenerateCodeService {

    @Autowired
    private ConfigGenerateCodeMapper configGenerateCodeMapper;
    @Autowired
    private ConfigFormatLinkFeatureRepository configFormatLinkFeatureRepository;
    @Autowired
    private SqliteUtils sqliteUtils;

    @Override
    public String getNextCode(PnConfigConstants.CODE_TYPE codeType) {
        String prefix = codeType.getPrefix();
        Integer length = codeType.getLength();
        return configGenerateCodeMapper.getGenerateNextCode(prefix, length);
    }

    @Override
    @Transactional
    public int syncSqliteMaxCodeToDb(PnConfigConstants.CODE_TYPE codeType) {
        String prefix = codeType.getPrefix();
        String sqliteTableName = codeType.getSqliteTableName();
        sqliteUtils.initSqliteConnection();
        String sql = "SELECT MAX(REPLACE(CODE,'"+ prefix +"','')) AS MAXCODE FROM " + sqliteTableName + " WHERE code LIKE '" + prefix + "%'";
        Map unique = sqliteUtils.unique(sql);
        if (MapUtil.isNotEmpty(unique)) {
            Integer maxcode = Convert.toInt(unique.get("MAXCODE"));
            ConfigGenerateCode generateCode = new ConfigGenerateCode();
            generateCode.setCode(prefix + maxcode);
            generateCode.setType(prefix);
            generateCode.setNum(maxcode);
            int delete = configGenerateCodeMapper.delete(
                    Wrappers.<ConfigGenerateCode>lambdaQuery()
                            .eq(ConfigGenerateCode::getType, prefix)
            );
            configGenerateCodeMapper.insert(generateCode);
        }
        return 1;
    }

    @Override
    public boolean moveToFormatForm(String moveId, String nextId, String prevId) throws ParseException {
        if(StringUtil.isNullOrEmpty(moveId)){
            return false;
        }
        if(StringUtil.isNullOrEmpty(nextId)){
            if(StringUtil.isNullOrEmpty(prevId)){
                return false;
            }else{
                //上移
                ConfigFormatLinkElement findByFeature = configFormatLinkFeatureRepository.findByFeature(moveId);
                ConfigFormatLinkElement findByFeature1 = configFormatLinkFeatureRepository.findByFeature(prevId);
                Integer sorting = findByFeature.getSorting();
                Integer sorting1 = findByFeature1.getSorting();
                findByFeature.setSorting(sorting1);
                findByFeature1.setSorting(sorting);
                configFormatLinkFeatureRepository.update(findByFeature);
                configFormatLinkFeatureRepository.update(findByFeature1);
            }

        }else{
            //下移
            ConfigFormatLinkElement findByFeature = configFormatLinkFeatureRepository.findByFeature(moveId);
            ConfigFormatLinkElement findByFeature1 = configFormatLinkFeatureRepository.findByFeature(nextId);
            Integer sorting = findByFeature.getSorting();
            Integer sorting1 = findByFeature1.getSorting();
            findByFeature.setSorting(sorting1);
            findByFeature1.setSorting(sorting);
            configFormatLinkFeatureRepository.update(findByFeature);
            configFormatLinkFeatureRepository.update(findByFeature1);
        }
        return true;
    }
}
